package com.ruoyi.common.rest;


import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Component
public abstract class RestTemplateUtil {

    private static final Logger log = LoggerFactory.getLogger(RestTemplateUtil.class);
    @Autowired
    private RestTemplate restTemplate;

    public JSONObject get(String url) {
        //get请求2  动态匹配值
//		String url = "http://localhost:9080/flowable/app/rest/models?filter={filter}&modelType={modelType}&sort={sort}";
//		Map<String, Object> varMap = new HashMap<String, Object>();
//		varMap.put("filter", "processes");
//		varMap.put("modelType", 0);
//		varMap.put("sort", "modifiedDesc");
//		ResponseEntity<String> res =  restTemplate.getForEntity(queryModelList_url, String.class, varMap);
//		String jsonStr = res.getBody();
        //get请求
        ResponseEntity<String> res = restTemplate.getForEntity(url, String.class);
        JSONObject json = JSON.parseObject(res.getBody());
        return json;
    }

    public JSONObject post(String url, Map<String,Object> params){
        String paramsJson = JSON.toJSONString(params);
        //post请求
        String result = restTemplate.postForObject(url, paramsJson, String.class);
        JSONObject json = JSON.parseObject(result);
        return json;
    }

    /**
     * 一般用作修改
     * 如 String uri = "http://localhost:8080/springrestexample/employees/{id}";
     *     Map<String, String> params = new HashMap<String, String>();
     *     params.put("id", "2");
     *     EmployeeVO updateParams = new EmployeeVO(2, "New Name", "Gilly", "test@email.com");
     * @param url
     * @param params
     */
    public void put(String url, Map<String,Object> updateParams, Map<String,Object> params){
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.put(url, updateParams, params);
    }

    /**
     * 用作删除
     * 如:url = "http://localhost:8080/springrestexample/employees/{id}";
     * Map<String, String> params = new HashMap<String, String>();
     * params.put("id", "2");
     * @param url
     * @param params
     */
    public void delete(String url, Map<String, String> params){
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.delete(url,  params );
    }

    public AjaxResult doPost(String url, Object object){
        String paramsJson = JSONUtil.toJsonStr(object);
        log.info(">>>>请求第三方服务[" + url + "]请求报文:" + paramsJson);
        String jsonStr = HttpUtil.post(url, paramsJson);
        log.info("<<<<请求第三方服务[" + url + "]响应报文:" + jsonStr);
        return JSONUtil.toBean(jsonStr, AjaxResult.class);
    }

    public Map<String,Object> doPostOfbizQuery(String url, Object object){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        Map<String,Object> context = new HashMap<>();
        context.put("header",user);
        context.put("body",object);
        String paramsJson = JSONUtil.toJsonStr(context);
        log.info(">>>>请求ofbiz服务[" + url + "]请求报文:" + paramsJson);
        String jsonStr = HttpUtil.post(url, paramsJson);
        log.info("<<<<请求ofbiz服务[" + url + "]响应报文:" + jsonStr);
        return JSONUtil.toBean(jsonStr, Map.class);
    }

    public int doPostOfbizUpdate(String url, Object object){
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        //content.setCreatedByUserLogin(String.valueOf(user.getUserId()));
        Map<String,Object> context = new HashMap<>();
        context.put("header",user);
        context.put("body",object);
        String paramsJson = JSONUtil.toJsonStr(context);
        log.info(">>>>请求ofbiz服务[" + url + "]请求报文:" + paramsJson);
        String jsonStr = HttpUtil.post(url, paramsJson);
        log.info("<<<<请求ofbiz服务[" + url + "]响应报文:" + jsonStr);
        cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(jsonStr);
        if (jsonObject != null && "success".equals(jsonObject.get("responseMessage"))) {
            return 1;
        }
        return 0;
    }


}
